package cn.northsea.service.impl;

import cn.northsea.mapper.ProductMapper;
import cn.northsea.pojo.*;
import cn.northsea.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<MaterialVo> findMaterialAll(String likename, String start, String end) {
        return productMapper.findMaterialAll(likename, start, end);
    }

    @Override
    public List<ProductVo> findProductAll(String likename, String start, String end) {
        return productMapper.findProductAll(likename, start, end);
    }

    @Override
    public List<ProductPlanVo> findProductPlanAll(String likename, String start, String end) {
        return productMapper.findProductPlanAll(likename, start, end);
    }

    @Override
    public List<MaterialProductVo> toFindMaterial(Integer productId, Integer productPlanId) {
        return productMapper.toFindMaterial(productId, productPlanId);
    }

    @Override
    public List<GetMaterialVo> getMaterial(String likename, String start, String end) {
        return productMapper.getMaterial(likename, start, end);
    }

    @Override
    public List<MaterialCategory> findMaterialCategoryAll() {
        return productMapper.findMaterialCategoryAll();
    }

    @Override
    public Material check(String materialName) {
        return productMapper.check(materialName);
    }

    @Override
    public void addMaterial(Material material) {
        material.setCreateAt(new Date(System.currentTimeMillis()));
        productMapper.addMaterial(material);
    }

    @Override
    public Material checkMaterialCategoryName(String materialCategoryName) {
        return productMapper.checkMaterialCategoryName(materialCategoryName);
    }

    @Override
    public void addMaterialCategory(MaterialCategory materialCategory) {
        materialCategory.setCreateAt(new Date(System.currentTimeMillis()));
        productMapper.addMaterialCategory(materialCategory);
    }

    @Override
    public void delMaterial(Integer[] ids) {
        productMapper.delMaterial(ids);
    }

    @Override
    public Material findMaterialByMid(Integer materialId) {
        return productMapper.findMaterialByMid(materialId);
    }

    @Override
    public void updateMaterial(Material material) {
        material.setCreateAt(new Date(System.currentTimeMillis()));
        productMapper.updateMaterial(material);
    }

    @Override
    public List<ProductCategory> findProductCategoryAll() {
        return productMapper.findProductCategoryAll();
    }

    @Override
    public Product checkProductName(String productName) {
        return productMapper.checkProductName(productName);
    }

    @Override
    public Product checkProductCategoryName(String productCategoryName) {
        return productMapper.checkProductCategoryName(productCategoryName);
    }

    @Override
    public void addProduct(Product product) {
        product.setCreateAt(new Date(System.currentTimeMillis()));
        productMapper.addProduct(product);
    }

    @Override
    public void addProductCategory(ProductCategory productCategory) {
        productCategory.setCreateAt(new Date(System.currentTimeMillis()));
        productMapper.addProductCategory(productCategory);
    }

    @Override
    public void delProduct(Integer[] ids) {
        productMapper.delProduct(ids);
    }

    @Override
    public Product findProductByPid(Integer productId) {
        return productMapper.findProductByPid(productId);
    }

    @Override
    public void updateProduct(Product product) {
        product.setCreateAt(new Date(System.currentTimeMillis()));
        productMapper.updateProduct(product);
    }

    @Override
    public List<Product> findProductList() {
        return productMapper.findProductList();
    }

    @Override
    public void addProductPlan(ProductPlan productPlan) {
        productPlan.setCreateAt(new Date(System.currentTimeMillis()));
        productMapper.addProductPlan(productPlan);
    }

    @Override
    public void delProductPlan(Integer productPlanId) {
        productMapper.delProductPlan(productPlanId);
    }

    @Override
    public ProductPlan findProductPlanById(Integer productPlanId) {
        return productMapper.findProductPlanById(productPlanId);
    }

    @Override
    public void updateProductPlan(ProductPlan productPlan) {
        productPlan.setCreateAt(new Date(System.currentTimeMillis()));
        productMapper.updateProductPlan(productPlan);
    }

    @Override
    public List<Material> findMaterialList() {
        return productMapper.findMaterialList();
    }

    @Override
    public void addProductMaterial(MaterialProduct materialProduct) {
        materialProduct.setCreateAt(new Date(System.currentTimeMillis()));
        materialProduct.setDeleted(0);
        materialProduct.setStatus(0);
        productMapper.addProductMaterial(materialProduct);
    }

    @Override
    public void delProductMaterial(Integer[] ids) {
        productMapper.delProductMaterial(ids);
    }

    @Override
    public MaterialProduct findProductMaterialById(Integer materialProductId) {
        return productMapper.findProductMaterialById(materialProductId);
    }

    @Override
    public void toUpdateProductMaterial(MaterialProduct materialProduct) {
        materialProduct.setCreateAt(new Date(System.currentTimeMillis()));
        productMapper.toUpdateProductMaterial(materialProduct);
    }

    @Override
    public void certainMaterial(Integer getMaterialId) {
        productMapper.certainMaterial(getMaterialId);
    }

    @Override
    @Transactional
    public void certainProductMaterial(MaterialProduct materialProduct) {
        List<GetMaterial> getMaterialList = productMapper.findGetMaterialByMid(materialProduct.getMaterialId());
        for (GetMaterial getMaterial : getMaterialList) {
            int i = productMapper.updateGetMaterial(materialProduct.getMaterialQuantity(), getMaterial.getGetMaterialId());
            if (i != 0) {
                productMapper.updateMaterialProduct(materialProduct);
                break;
            }
        }
    }

    @Override
    public String findGetMaterialQuantity(GetMaterial getMaterial, Integer quantity) {
        Double quantity1 = getMaterial.getMaterialQuantity() * quantity;
        List<GetMaterial> getMaterialList = productMapper.findGetMaterialByMid(getMaterial.getMaterialId());
        for (GetMaterial material : getMaterialList) {
            if (material.getMaterialQuantity() >= quantity1) {
                return "OK";
            }
        }
        return "ERROR";
    }

    @Override
    @Transactional
    public String checkProduct(Integer productId, Integer productPlanId) {
        List<MaterialProduct> materialProductList = productMapper.findMaterialProductByPid(productId, productPlanId);
        List<MaterialProduct> materialProductList1 = productMapper.findMaterialProductByPid2(productId, productPlanId);
        if (materialProductList1 != null && materialProductList != null) {
            if (materialProductList1.size() == materialProductList.size()) {
                return "OK";
            } else return "ERROR";
        }
        return "ERROR";
    }

    @Override
    public void toProduct(Integer productPlanId) {
        productMapper.toProduct(productPlanId);
    }

    @Override
    @Transactional
    public ProductPlan ProductPlanOver(Integer productPlanId) {
        productMapper.ProductPlanOver(productPlanId);
        ProductPlan productPlan = productMapper.findProductPlanById(productPlanId);
        return productPlan;
    }

    @Override
    public void getPid(Integer productPlanId) {
        long pid = System.currentTimeMillis();
        productMapper.getPid(productPlanId, pid);
    }

    @Override
    public List<Warehouse> findWarehouseAll() {
        return productMapper.findWarehouseAll();
    }

    @Override
    @Transactional
    public void InWarehouse(WarehouseIn warehouseIn) {
        warehouseIn.setWarehouseInTime(new Date(System.currentTimeMillis()));
        productMapper.InWarehouse(warehouseIn);
        List<WarehouseProduct> warehouseProductList = productMapper.findWarehouseProductAll();
        int i = 0;
        for (WarehouseProduct warehouseProduct : warehouseProductList) {
            if (warehouseProduct.getProductId() == warehouseIn.getProductId()) {
                i++;
            }
        }
        if (i == 1) {
            for (WarehouseProduct warehouseProduct : warehouseProductList) {
                if (warehouseProduct.getProductId() == warehouseIn.getProductId() && warehouseProduct.getWarehouseId() == warehouseIn.getWarehouseId()) {
                    int quantity = warehouseProduct.getWarehouseProductQuantity() + warehouseIn.getQuantity();
                    warehouseProduct.setWarehouseProductQuantity(quantity);
                } else if (warehouseProduct.getProductId() == warehouseIn.getProductId() && warehouseProduct.getWarehouseId() != warehouseIn.getWarehouseId()) {
                    productMapper.inWarehouseProduct(warehouseIn.getProductId(),
                            warehouseIn.getQuantity(), warehouseIn.getWarehouseId());
                }
            }
        } else {
            productMapper.inWarehouseProduct(warehouseIn.getProductId(),
                    warehouseIn.getQuantity(), warehouseIn.getWarehouseId());
        }
    }

    @Override
    public void toAddGetMaterial(GetMaterial getMaterial) {
        getMaterial.setCreateAt(new Date(System.currentTimeMillis()));
        getMaterial.setStatus(0);
        productMapper.toAddGetMaterial(getMaterial);
    }

}
